const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const VersionFile = require('webpack-version-file');

module.exports = {

  mode: 'development',

  // devtool: 'inline-source-map',

  entry: ['./src/index.ts'],

  output: {
    filename: '[name].bundle.js',
    chunkFilename: '[name].js',
    path: path.resolve(__dirname, 'dist'),
  },

  resolve: {
    extensions: ['.js', '.ts', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
    }
  },

  plugins: [
    new VersionFile({
      output: '../tools/version.txt',
      templateString: '<%= buildDate %>',
      data: {
        buildDate: Date.parse(new Date())
      }
    }),

    new webpack.BannerPlugin(`JoinQuant - ${ Date.parse(new Date()) }`),

    new CleanWebpackPlugin(['dist']),

    new MiniCssExtractPlugin({
      filename: `[name].bundle.css`
    }),

    new HtmlWebpackPlugin({
      chunks: [],
      inject: 'body',
      filename: 'index.html',
      template: './src/index.html'
    })
  ],

  externals: {},

  module: {
    rules: [
      {
        test: /\.js$/,
        loader: "babel-loader",
        exclude: /node_modules/
      },

      {
        test: /\.ts$/,
        exclude: /node_modules/,
        enforce: "pre",
        loader: "tslint-loader"
      },

      {
        test: /\.(tsx|ts)$/,
        use: "ts-loader",
        exclude: /node_modules/
      },

      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader',
          'less-loader',
          'postcss-loader'
        ]
      },

      {
        test: /\.less$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'less-loader'
        ]
      },

      {
        test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
        use: [{
          loader: 'url-loader?limit=8192&name=asset/images/[name].[hash:8].[ext]',
          options: {
            limit: 10000
          }
        }]
      },

      {
        test: /\.html$/,
        loader: "html-loader"
      }
    ]
  }
}
